package com.sky.ContactManager;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class Transfert {
	//public static final String domaine="192.168.1.4";
	// Mettre l'adresse du script PHP
	// Attention localhost ou 127.0.0.1 ne fonctionnent pas. Mettre l'adresse IP local.
	public static final String strURL1 = "http://192.168.1.4/ContactManager/web/webservice.php/main/produits";
	public static final String strURL2 = "http://192.168.1.4/ContactManager/web/webservice.php/main/contacts";
	public static final String strURL3 = "http://192.168.1.4/ContactManager/web/webservice.php/main/commandes";
	public static final String strURL4 = "http://192.168.1.4/ContactManager/web/webservice.php/main/verification";
	public static final String strURL5 = "http://192.168.1.4/ContactManager/web/webservice.php/main/coord";
    public Main context;
	
	public Transfert(Main context)
	{
		this.context=context;
	}
	public Transfert()
	{
		
	}
	
	public String ServerData() {
		InputStream is = null;
		String result = "";
		
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		
		nameValuePairs.add(new BasicNameValuePair("login",Base64.encodeBytes(this.context.login.getText().toString().getBytes())));
		Log.d("pass", this.context.motdepasse.getText().toString());
		nameValuePairs.add(new BasicNameValuePair("motdepasse",Base64.encodeBytes(this.context.motdepasse.getText().toString().getBytes())));
		context.motdepasse.setText("");
		context.login.setText("");
		Log.d("main","73" );
		
		try{
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(strURL2);
			
				
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
			Log.w("main", "158");

		}catch(Exception e){
			Log.e("log_tag", "Error in http connection " + e.toString());
			
		}
		
		// Convertion de la requête en string
		try{Log.w("main", "166");
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result=sb.toString();
			Log.w("main", "175");
			Log.w("result",result );
		}catch(Exception e){
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		
		
		
		
		
		
	return result;
	
}
	
}
